import Cookies from 'js-cookie';
import { getListForDecoration } from '@/api/domain';
import axios from 'axios';

const app = {
    state: {
        sidebar: {
            opened: Cookies.get('sidebarStatus') ? !!+Cookies.get('sidebarStatus') : true,
            withoutAnimation: false
        },
        device: 'desktop',
        language: Cookies.get('language') || 'en',
        size: Cookies.get('size') || 'medium',
        sort: {},
        delId: null,
        mobileMenuVisible: false,
        isMobile: false
    },
    mutations: {
        TOGGLE_SIDEBAR: state => {
            state.sidebar.opened = !state.sidebar.opened;
            state.sidebar.withoutAnimation = false;
            if (state.sidebar.opened) {
                Cookies.set('sidebarStatus', 1);
            } else {
                Cookies.set('sidebarStatus', 0);
            }
        },
        CLOSE_SIDEBAR: (state, withoutAnimation) => {
            Cookies.set('sidebarStatus', 0);
            state.sidebar.opened = false;
            state.sidebar.withoutAnimation = withoutAnimation;
        },
        TOGGLE_DEVICE: (state, device) => {
            state.device = device;
        },
        SET_LANGUAGE: (state, language) => {
            state.language = language;
            Cookies.set('language', language);
        },
        SET_SIZE: (state, size) => {
            state.size = size;
            Cookies.set('size', size);
        },
        SET_SORT: (state, data) => {
            state.sort = data;
        },
        SET_DEL_ID: (state, data) => {
            state.delId = data;
        },
        TOGGLE_MOBILE_MENU: state => {
            state.mobileMenuVisible = !state.mobileMenuVisible;
            console.log('TOGGLE_MOBILE_MENU mutation called, new state:', state.mobileMenuVisible);
        },
        SET_MOBILE_MENU: (state, visible) => {
            state.mobileMenuVisible = visible;
            console.log('SET_MOBILE_MENU mutation called, new state:', state.mobileMenuVisible);
        },
        SET_IS_MOBILE: (state, isMobile) => {
            state.isMobile = isMobile;
            console.log('SET_IS_MOBILE mutation called, new state:', state.isMobile);
        }
    },
    actions: {
        ToggleSideBar: ({ commit }) => {
            commit('TOGGLE_SIDEBAR');
        },
        CloseSideBar ({ commit }, { withoutAnimation }) {
            commit('CLOSE_SIDEBAR', withoutAnimation);
        },
        ToggleDevice ({ commit }, device) {
            commit('TOGGLE_DEVICE', device);
        },
        CLEAR_FRONT_CACHE ({ commit, rootState }) {
            const siteId = rootState.user.siteId;
            getListForDecoration(siteId).then(res => {
                res.data.map(s => {
                    axios.get(`https://${s.domainName}/refreshCacheCN`);
                    axios.get(`https://${s.domainName}/refreshCacheUS`);
                });
            });
        },
        toggleMobileMenu ({ commit }) {
            console.log('toggleMobileMenu action called');
            commit('TOGGLE_MOBILE_MENU');
        },
        setMobileMenu ({ commit }, visible) {
            console.log('setMobileMenu action called, parameter:', visible);
            commit('SET_MOBILE_MENU', visible);
        },
        setLanguage ({ commit }, language) {
            commit('SET_LANGUAGE', language);
        },
        setSize ({ commit }, size) {
            commit('SET_SIZE', size);
        },
        setIsMobile ({ commit }, isMobile) {
            console.log('setIsMobile action called, parameter:', isMobile);
            commit('SET_IS_MOBILE', isMobile);
            return isMobile;
        },
        checkMobile ({ commit, state }) {
            console.log('checkMobile action called');
            return new Promise((resolve) => {
                const isMobileDevice = () => {
                    const userAgent = navigator.userAgent.toLowerCase();
                    const mobileRegex = /android|webos|iphone|ipad|ipod|blackberry|iemobile|opera mini/i;
                    const isMobileByAgent = mobileRegex.test(userAgent);
                    const isMobileByWidth = window.innerWidth <= 768;

                    return isMobileByAgent || isMobileByWidth;
                };

                const newIsMobileValue = isMobileDevice();
                console.log('检测到移动设备状态:', newIsMobileValue, '当前状态:', state.isMobile);

                if (state.isMobile !== newIsMobileValue) {
                    commit('SET_IS_MOBILE', newIsMobileValue);
                }

                resolve(newIsMobileValue);
            });
        }
    }
};

export default app;
